package dime.commons;

import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
import dime.Application;

public class BaseScreen extends MainScreen {

	private LabelField titleField;

	public BaseScreen(long style) {
		super(style);
	}

	public BaseScreen(String title) {
		setTitle(titleField = new LabelField(title, LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH));
	}

	public BaseScreen(String title, long style) {
		this(style);
		setTitle(titleField = new LabelField(title, LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH));
	}

	public boolean keyChar(char key, int status, int time) {
		/* Intercept the ESC key and exit the application. */
		boolean retval = false;
		switch (key) {
		case Characters.ESCAPE:
			int screenCount = Application.getUiApplication().getScreenCount();

			if (screenCount <= 1) {
				System.exit(0);
			}
			else {
				if (!onEscape()) {
					Application.getUiApplication().popScreen(this);
				}
			}

			retval = true;
			break;

		default:
			return super.keyChar(key, status, time);
		}
		return retval;
	}

	public boolean onEscape() {
		return false;
	}

	public void setTitle(String title) {
		if (titleField != null) {
			titleField.setText(title);
		}
		super.setTitle(title);
	}

}
